const path = require('path')
const merge = require('webpack-merge')
const FileManagerPlugin = require('filemanager-webpack-plugin')
const fs = require('fs')
const customTheme = require('./van-custom-theme')
const webpack = require('webpack')

function resolve (dir) {
  return path.join(__dirname, dir)
}

module.exports = {
  outputDir: process.env.outputDir,
  assetsDir: 'static',
  indexPath: 'index.html',
  productionSourceMap: false,
  configureWebpack: {
     plugins: [
        new webpack.ProvidePlugin({
          $:"jquery",
          jQuery:"jquery",
          "windows.jQuery":"jquery"
        })
      ]
    },
  devServer: {
    port: 8080,
    proxy: {
      '/bnd-uaac': {
        target: 'http://sltest.shanglike.com:8200',
        ws: true,
        changeOrigin: true,
        pathRewrite: {
        }
      },
    }
  },

  publicPath: process.env.NODE_ENV === 'production'
    ? './' : '/',
  configureWebpack: (config) => {
    config.resolve = {
      extensions: ['.js', '.vue', '.json', '.css', '.less'],
      alias: {
        '@': resolve('src'),
        'components': resolve('src/components'),
        'images': resolve('src/assets/images'),
        'styles': resolve('src/assets/styles'),
        'fetch': resolve('src/fetch'),
        'utils': resolve('src/utils')
      }
    }
    config.externals = {
      BMap: 'BMap'
    }
    if (process.env.NODE_ENV === 'production') {
      // 为生产环境修改配置
      const zipConfig = require('./appVersion/' + process.env.VUE_APP_MODE + '.zip')
      const dateTime = new Date()
      zipConfig.build = dateTime.toLocaleString()
      // 关闭webpack性能错误提示
      config.performance = {
        hints: false
      }
      // 每次打包编译版本号自动加1
      const versionArray = zipConfig.version.split('.').reverse()
      for (let i = 0; i < versionArray.length; i++) {
        if (versionArray[i] - 9 < 0) {
          versionArray[i]++
          zipConfig.version = versionArray.reverse().join('.')
          break
        } else {
          versionArray[i] = 0
        }
      }
      config.plugins.push(
        new FileManagerPlugin({
          onEnd: {
            delete: [
              process.env.outputDir + '.zip'
            ],
            archive: [
              { source: process.env.outputDir, destination: process.env.outputDir + '.zip' }
            ]
          }
        })
      )
      config.plugins.push({
        apply: (compiler) => {
          compiler.hooks.done.tap('MyPlugin', () => {
            fs.writeFileSync(process.env.outputDir + '/config.json', JSON.stringify(zipConfig))
            fs.writeFileSync('./appVersion/' + process.env.VUE_APP_MODE + '.zip.json', JSON.stringify(zipConfig))
          })
        }
      })
    }
  },
  // 对内部的 webpack 配置（比如修改、增加Loader选项）(链式操作)
  chainWebpack: config => {
    // 小于5000kb的图片自动转为base64
    config.module
      .rule('images')
      .test(/\.(png|jpe?g|gif|webp)(\?.*)?$/)
      .use('url-loader')
      .loader('url-loader')
      .tap(options =>
        merge(options, {
          limit: 500000
        })
      )
  },
  // css的处理
  css: {
    // 当为true时，css文件名可省略 module 默认为 false
    modules: false,
    // 是否将组件中的 CSS 提取至一个独立的 CSS 文件中,当作为一个库构建时，你也可以将其设置为 false 免得用户自己导入 CSS
    // 默认生产环境下是 true，开发环境下是 false
    extract: process.env.NODE_ENV !== 'development',
    // 是否为 CSS 开启 source map。设置为 true 之后可能会影响构建的性能
    sourceMap: true,
    // 向 CSS 相关的 loader 传递选项(支持 css-loader postcss-loader sass-loader less-loader stylus-loader)
    loaderOptions: {
      less: {
        modifyVars: customTheme
      }
    }
  },
  // 是否在开发环境下通过 eslint-loader 在每次保存时 lint 代码 (在生产构建时禁用 eslint-loader)
  lintOnSave: process.env.NODE_ENV !== 'production'
  // lintOnSave: 'error'
}
